﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Presentation.Children;
using Presentation;
using Presentation.Reports;
using BusinessObjects;
using Presentation.Reports.MonthInvoice;
using BusinessLogic;
using Common;
using DCMSExceptions;
using System.Net.Mail;

namespace DaycareSystem {
  /// <summary>
  /// Parent Mdi form
  /// </summary>
  public partial class MainForm : Form {

    #region - Constructors -

    /// <summary>
    /// Initializes a new instance of the <see cref="MainForm"/> class.
    /// </summary>
    public MainForm( ) {
      InitializeComponent( );
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="MainForm"/> class.
    /// </summary>
    /// <param name="firstRun">if set to <c>true</c> [first run].</param>
    public MainForm( bool firstRun )
      : this( ) {
      if ( firstRun ) {
        FirstRunForm firstForm = new FirstRunForm( );
        firstForm.ShowDialog( );
      }
    }

    #endregion

    #region - Events -

    private void editUserMenuItem_Click( object sender, EventArgs e ) {
      if ( !FormIsOpen( typeof( ChangePasswordForm ) ) ) {
        ChangePasswordForm form = new ChangePasswordForm( );
        form.MdiParent = this;
        form.Show( );
      }
    }

    private void parametersMenuItem_Click( object sender, EventArgs e ) {
      if ( !FormIsOpen( typeof( SettingsForm ) ) ) {
        SettingsForm form = new SettingsForm( );
        form.MdiParent = this;
        form.Show( );
      }

    }

    private void newChildFicheMenuItem_Click( object sender, EventArgs e ) {

      CloseForms( typeof( ContractsForm ) );

      Child newChild = new Child( );
      Children list = new Children( );
      list.Add( newChild );
      ContractsForm newContractForm = new ContractsForm( list, 0, false );
      newContractForm.MdiParent = this;
      newContractForm.Show( );
    }

    private void openMenuItem_Click( object sender, EventArgs e ) {

      if ( !FormIsOpen( typeof( ContractsForm ) ) ) {
        ChildrenListForm listForm = new ChildrenListForm( false /*multiselect = false*/ );

        if ( listForm.ShowDialog( ) == System.Windows.Forms.DialogResult.OK ) {
          if ( listForm.Children.Count > 0 ) {
            ContractsForm contractsForm = new ContractsForm( listForm.Children, listForm.SelectedPosition, listForm.AllChildren );
            contractsForm.MdiParent = this;
            contractsForm.Show( );
          } else {
            MessageBox.Show( "Er zijn nog geen actieve kinderen.", "Kinderfiches", MessageBoxButtons.OK, MessageBoxIcon.Information );
          }
        }
      }
    }

    private void postcodesToolStripMenuItem_Click( object sender, EventArgs e ) {
      if ( !FormIsOpen( typeof( NewZipcodeForm ) ) ) {
        NewZipcodeForm newZipcodeForm = new NewZipcodeForm( );
        newZipcodeForm.MdiParent = this;
        newZipcodeForm.Show( );
      }
    }

    private void dairyToolStripMenuItem_Click( object sender, EventArgs e ) {
      Children children = new ChildrenBll( ).GetChildren( true /*allChildren = true*/);
      if ( children.Count > 0 ) {
        if ( !FormIsOpen( typeof( ChildDiaryForm ) ) ) {
          ChildDiaryForm form = new ChildDiaryForm( );
          form.MdiParent = this;
          form.Show( );
        }
      } else {
        MessageBox.Show( "Er zijn nog geen kinderen aangemaakt.", "Dagboek", MessageBoxButtons.OK, MessageBoxIcon.Information );
      }
    }

    private void childfichesReportMenuItem_Click( object sender, EventArgs e ) {
      if ( !FormIsOpen( typeof( ChildficheReportSelectionForm ) ) ) {
        ChildficheReportSelectionForm form = new ChildficheReportSelectionForm( );
        form.MdiParent = this;
        form.Show( );
      }
    }

    private void invoiceReportMenuItem_Click( object sender, EventArgs e ) {
      if ( !FormIsOpen( typeof( MonthInvoiceSelectionForm ) ) ) {
        MonthInvoiceSelectionForm form = new MonthInvoiceSelectionForm( );
        form.MdiParent = this;
        form.Show( );
      }
    }

    private void childficheButton_Click( object sender, EventArgs e ) {
      if ( !FormIsOpen( typeof( ContractsForm ) ) ) {
        ChildrenListForm listForm = new ChildrenListForm( false /*multiselect = false*/ );
        if ( listForm.ShowDialog( ) == System.Windows.Forms.DialogResult.OK ) {
          ContractsForm contractsForm = new ContractsForm( listForm.Children, listForm.SelectedPosition, listForm.AllChildren );
          contractsForm.MdiParent = this;
          contractsForm.Show( );
        }
      }
    }

    private void diaryButton_Click( object sender, EventArgs e ) {
      if ( !FormIsOpen( typeof( ChildDiaryForm ) ) ) {
        ChildDiaryForm form = new ChildDiaryForm( );
        form.MdiParent = this;
        form.Show( );
      }
    }

    private void reportingToolbarButton_ButtonClick( object sender, EventArgs e ) {
      if ( !reportingToolbarButton.IsOnDropDown ) {
        reportingToolbarButton.ShowDropDown( );
      }
    }

    private void overToolStripMenuItem_Click( object sender, EventArgs e ) {
      if ( !FormIsOpen( typeof( AboutDaycareSystem ) ) ) {
        AboutDaycareSystem form = new AboutDaycareSystem( );
        form.ShowDialog( );
      }
    }

    private void exitToolStripMenuItem_Click( object sender, EventArgs e ) {
      Application.Exit( );
    }

    #endregion

    #region - Methods -

    private bool FormIsOpen( Type formType ) {
      bool isFormOpen = false;
      foreach ( Form form in this.MdiChildren ) {
        if ( form.GetType( ).Equals( formType ) ) {
          isFormOpen = true;
          form.Focus( );
        }
      }
      return isFormOpen;
    }

    private void CloseForms( Type formType ) {
      foreach ( Form form in this.MdiChildren ) {
        if ( form.GetType( ).Equals( formType ) ) {
          form.Close( );
        }
      }
    }

    #endregion
    
  }
}
